<?php

class Webmix_AdminWebmixController extends Core_Controller_Action_Admin {
	public function indexAction() {
		
        $this->view->navigation = $navigation = Engine_Api::_()->getApi('menus', 'core')
      		->getNavigation('webmix_admin_main', array(), 'webmix_admin_main_webmix');
		
      	$this->view->formFilter = $formFilter = new Webmix_Form_Admin_Webmix_Filter();
	    $page = $this->_getParam('page', 1);
	
	    $table = Engine_Api::_()->getDbtable('webmixes', 'webmix');
	    $select = $table->select()
                        ->where('parent_id = ?',0)
                        ;
        
        //count webmix added times
        $parents = $table->fetchAll($select);
        $use_count = array();
        foreach($parents as $parent){
            $childSelect = $table->select()
                        ->where('parent_id = ?',$parent->webmix_id)
                        ;
            $childCount = $table->fetchAll($childSelect)->count();
            $use_count[$parent->webmix_id] = $childCount + 1;
        }
        $this->view->use_count = $use_count;
	    // Process form
	    $values = array();
	    if( $formFilter->isValid($this->_getAllParams()) ) {
	      $values = $formFilter->getValues();
	    }

	    foreach( $values as $key => $value ) {
	      if( null === $value ) {
	        unset($values[$key]);
	      }
	    }
	
	    $values = array_merge(array(
	      'order' => 'webmix_id',
	      'order_direction' => 'DESC',
	    ), $values);
	    
	    $this->view->assign($values);
//	
//	    // Set up select info
	    $select->order(( !empty($values['order']) ? $values['order'] : 'webmix_id' ) . ' ' . ( !empty($values['order_direction']) ? $values['order_direction'] : 'DESC' ));
	
	    if( !empty($values['title']) ) {
	      $select->where('title LIKE ?', '%' . $values['title'] . '%');
	    }
	    if( !empty($values['country']) ) {
	      $select->where('country_id = ?', $values['country']);
	    }

	    if( !empty($values['createby']) ) {
	      $select->where('type = ?', $values['createby']);
	    }
		if( !empty($values['owner']) ) {
	      $select->from($table->info('name'))
	      		->join('engine4_users as u', 'u.user_id = '.$table->info('name') .'.owner_id', null)
	      		->where('u.displayname LIKE "%'.$values['owner'].'%" or u.username LIKE "%'.$values['owner'].'%"');
	    }
	    
	    
//	    if( !empty($values['webmix_id']) ) {
//	      $select->where('webmix.wemix_id = ?', (int) $values['webmix_id']);
//	    }
	    


    // Make paginator
    $this->view->paginator = $paginator = Zend_Paginator::factory($select);
    $paginator->setItemCountPerPage(150);
    $this->view->paginator = $paginator->setCurrentPageNumber( $page );
    $this->view->formValues = $values;    
        
    }
    
  public function deleteAction()
  {
    $id = $this->_getParam('id', null);
    $this->view->webmix = $webmix = Engine_Api::_()->getItem('webmixes', $id);
    $this->view->form = $form = new Webmix_Form_Admin_Webmix_Delete();
    // deleting user
    //$form->user_id->setValue($id);

    if( $this->getRequest()->isPost() ) {
      $db = Engine_Api::_()->getDbtable('countries', 'webmix')->getAdapter();
      $db->beginTransaction();
	
      $webmixT = 	Engine_Api::_()->getDbtable('webmixes', 'webmix');
	  $blockT = 	Engine_Api::_()->getDbtable('blocks', 'webmix');
      try {
        
		$webmix_id = $webmix->getIdentity();
        $webmixT->delete(" parent_id = $webmix_id ");
		$blockT->delete(" webmix_id = $webmix_id ");
		
		$webmix->delete();
        $db->commit();
      } catch( Exception $e ) {
        $db->rollBack();
        throw $e;
      }
      
      return $this->_forward('success', 'utility', 'core', array(
        'smoothboxClose' => true,
        'parentRefresh' => true,
        'format'=> 'smoothbox',
        'messages' => array('This webmix has been successfully deleted.')
      ));
    }
  }
  
  public function multiModifyAction()
      {
        if( $this->getRequest()->isPost() ) {
          $values = $this->getRequest()->getPost();
          foreach ($values as $key=>$value) {
            if( $key == 'modify_' . $value ) {
              $company = Engine_Api::_()->getItem('webmixes', (int) $value);
              if( $values['submit_button'] == 'delete' ) {	            
              		$company->delete();
              }
            }
          }
        }	
        return $this->_helper->redirector->gotoRoute(array('action' => 'index'));
    }  
  public function editAction(){
        $this->view->navigation = $navigation = Engine_Api::_()->getApi('menus', 'core')
      		->getNavigation('webmix_admin_main', array(), 'webmix_admin_main_webmix');
            
        $webmix_id = $this->_getParam('webmix_id',0);
        if(empty($webmix_id)) {
            return $this->_forward('notfound');
        }
        $webmix = Engine_Api::_()->getItem('webmixes',$webmix_id);
        if(!$webmix){
             return $this->_forward('notfound');
        }
        $this->view->webmix = $webmix;
        $this->view->viewer = Engine_Api::_()->user()->getViewer();
        
  }
  
  public function defaultAction(){
        $webmix_id = $this->_getParam('webmix_id',0);
        if(empty($webmix_id)) {
           return $this->_forward('success', 'utility', 'core', array(
                'smoothboxClose' => true,
                'parentRefresh' => false,
                'messages' => array('Can not found your webmix.')
           ));
        }
        $webmix = Engine_Api::_()->getItem('webmixes',$webmix_id);
        if(!$webmix){
             return $this->_forward('success', 'utility', 'core', array(
                'smoothboxClose' => true,
                'parentRefresh' => false,
                'messages' => array('Can not found your webmix.')
              ));
        }
        $this->view->form  = $form = new Webmix_Form_Webmix_Default();
        if(!$this->getRequest()->isPost()){
            return;
        }
        
        $webmixTable = Engine_Api::_()->getDbtable('webmixes','webmix');
        $webmixTable->update(
            array('default'=>0)
            
        );
        
        $webmix->default = 1;
        $webmix->save();
        
        return $this->_forward('success', 'utility', 'core', array(
                'smoothboxClose' => true,
                'parentRefresh' => true,
                'messages' => array('This webmix had been successfully set default.')
        ));
  }
  
  
  public function publicAction(){
        $webmix_id = $this->_getParam('webmix_id',0);
        if(empty($webmix_id)) {
           return $this->_forward('success', 'utility', 'core', array(
                'smoothboxClose' => true,
                'parentRefresh' => false,
                'messages' => array('Can not found your webmix.')
           ));
        }
        $webmix = Engine_Api::_()->getItem('webmixes',$webmix_id);
        if(!$webmix){
             return $this->_forward('success', 'utility', 'core', array(
                'smoothboxClose' => true,
                'parentRefresh' => false,
                'messages' => array('Can not found your webmix.')
              ));
        }
        $this->view->form  = $form = new Webmix_Form_Webmix_Default();
        $form->setDescription($this->view->translate('Are you sure you want to publish this webmix ?'));
        $form->setTitle($this->view->translate('Publish webmix.'));
        if(!$this->getRequest()->isPost()){
            return;
        }
        
        $webmixTable = Engine_Api::_()->getDbtable('webmixes','webmix');
        
        $webmix->public = 1;
        $webmix->save();
        
        return $this->_forward('success', 'utility', 'core', array(
                'smoothboxClose' => true,
                'parentRefresh' => true,
                'messages' => array('This webmix had been successfully set publish.')
        ));
  }
  public function unpublicAction(){
        $webmix_id = $this->_getParam('webmix_id',0);
        if(empty($webmix_id)) {
           return $this->_forward('success', 'utility', 'core', array(
                'smoothboxClose' => true,
                'parentRefresh' => false,
                'messages' => array('Can not found your webmix.')
           ));
        }
        $webmix = Engine_Api::_()->getItem('webmixes',$webmix_id);
        if(!$webmix){
             return $this->_forward('success', 'utility', 'core', array(
                'smoothboxClose' => true,
                'parentRefresh' => false,
                'messages' => array('Can not found your webmix.')
              ));
        }
        $this->view->form  = $form = new Webmix_Form_Webmix_Default();
        $form->setDescription($this->view->translate('Are you sure you want to hide this webmix ?'));
        $form->setTitle($this->view->translate('Publish webmix.'));
        if(!$this->getRequest()->isPost()){
            
            return;
        }
        
        $webmixTable = Engine_Api::_()->getDbtable('webmixes','webmix');
        
        $webmix->public = 0;
        $webmix->save();
        
        return $this->_forward('success', 'utility', 'core', array(
                'smoothboxClose' => true,
                'parentRefresh' => true,
                'messages' => array('This webmix had been successfully set hiden.')
        ));
  }
      
}

